﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace RSIS.Editor
{
    public partial class MainHouseTypeTraitForm : Form
    {
        public FormType m_FormType;
        public string m_strHouseID;
        private System.Data.DataRow m_pDataRow;
        private System.Data.DataTable m_pMainHouseTypeTable;

        public System.Data.DataRow DataRow
        {
            set
            {
                m_pDataRow = value;
            }
        }

        public System.Data.DataTable MainHouseTypeTable
        {
            get
            {
                return m_pMainHouseTypeTable;
            }
            set
            {
                m_pMainHouseTypeTable = value;
            }
        }

        public MainHouseTypeTraitForm()
        {
            InitializeComponent();
        }

        public void MainHouseTypeTraitForm_Load(object sender, EventArgs e)
        {
            // 添加当前批次
            RSIS.DataBase.RealInfo pRealInfo = new RSIS.DataBase.RealInfo();
            Npgsql.NpgsqlConnection pADOCon = new Npgsql.NpgsqlConnection(pRealInfo.GetADOConStr());
            string strQueryString = "Select * from rsis_housebatch where houseid='" + m_strHouseID + "'";
            Npgsql.NpgsqlDataAdapter pSqlDataAdp = new Npgsql.NpgsqlDataAdapter();
            pSqlDataAdp.SelectCommand = new Npgsql.NpgsqlCommand(strQueryString, pADOCon);
            Npgsql.NpgsqlCommandBuilder pSqlComBuilder = new Npgsql.NpgsqlCommandBuilder(pSqlDataAdp);
            System.Data.DataSet pDataSet = new DataSet();
            pSqlDataAdp.Fill(pDataSet, "housebatch");
            DataTable pDataTable=pDataSet.Tables["housebatch"];
            cmbBatch.DataSource=pDataTable;
            cmbBatch.DisplayMember = "batchname";
            cmbBatch.ValueMember = "id";

            if (this.m_FormType == FormType.MOD)
            {
             

                //加载现有属性
                strQueryString = "Select * from rsis_mainhousetype";
                pSqlDataAdp.SelectCommand = new Npgsql.NpgsqlCommand(strQueryString, pADOCon);
                pSqlComBuilder = new Npgsql.NpgsqlCommandBuilder(pSqlDataAdp);

                pDataSet = new DataSet();
                pSqlDataAdp.Fill(pDataSet, "mainhousetype");
                pDataSet.Tables["mainhousetype"].PrimaryKey = new DataColumn[] { pDataSet.Tables["mainhousetype"].Columns["ID"] };
                m_pMainHouseTypeTable.PrimaryKey = new DataColumn[] { m_pMainHouseTypeTable.Columns["id"] };
                System.Data.DataRow pRow = m_pDataRow;//m_pMainHouseTypeTable.Rows.Find(m_strID);//pDataSet.Tables["mainhousetype"].Rows.Find(m_strID);


                //列出楼盘信息
                //列出楼盘信息
                if (pRow["HouseType"] != DBNull.Value)
                {
                    this.txtHouseType.Text = pRow["HouseType"].ToString();
                }

                if (pRow["HOUSETYPETRAIT"] != DBNull.Value)
                {
                    this.txtHOUSETYPETRAIT.Text = pRow["HOUSETYPETRAIT"].ToString();
                }

                if (pRow["HOUSEAREARANGE"] != DBNull.Value)
                {
                    this.txtHOUSEAREARANGE.Text = pRow["HOUSEAREARANGE"].ToString();
                }

                if (pRow["HOUSEAREARANGE2"] != DBNull.Value)
                {
                    this.txtHOUSEAREARANGE2.Text = pRow["HOUSEAREARANGE2"].ToString();
                }

                if (pRow["batch"] != DBNull.Value)
                {
                    this.cmbBatch.Text = pRow["batch"].ToString();
                }




            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnHOUSETYPETRAIT_Click(object sender, EventArgs e)
        {
            TextForm frmText = new TextForm();
            frmText.txtValue.Text = this.txtHOUSETYPETRAIT.Text;
            frmText.Text = this.lbHouseType.Text;
            if (this.m_FormType == FormType.VIEW)
            {
                frmText.btnOK.Enabled = false;
                frmText.btnOK.Visible = false;

            }

            if (frmText.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtHOUSETYPETRAIT.Text = frmText.txtValue.Text;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;

            //RSIS.DataBase.RealInfo pRealInfo = new RSIS.DataBase.RealInfo(); 


            //Npgsql.NpgsqlConnection pADOCon = new Npgsql.NpgsqlConnection(pRealInfo.GetADOConStr());
            //string strQueryString = "Select * from rsis_mainhousetype where houseid='"+m_strHouseID+"'";
            //Npgsql.NpgsqlDataAdapter pSqlDataAdp = new Npgsql.NpgsqlDataAdapter();
            //pSqlDataAdp.SelectCommand = new Npgsql.NpgsqlCommand(strQueryString, pADOCon);
            //Npgsql.NpgsqlCommandBuilder pSqlComBuilder = new Npgsql.NpgsqlCommandBuilder(pSqlDataAdp);

            //System.Data.DataSet pDataSet = new DataSet();

            //pSqlDataAdp.Fill(pDataSet, "mainhousetype");

            System.Data.DataRow pRow;
            if (this.m_FormType == FormType.NEW)
            {
                pRow = m_pMainHouseTypeTable.NewRow();//pDataSet.Tables["mainhousetype"].NewRow();
            }
            else
            {
               // m_pMainHouseTypeTable.PrimaryKey = new System.Data.DataColumn[] { m_pMainHouseTypeTable.Columns["id"] };
                pRow = m_pDataRow;// m_pMainHouseTypeTable.Rows.Find(m_strID);

            }

            pRow["HOUSEID"] = this.m_strHouseID;

            pRow["HOUSETYPE"] = this.txtHouseType.Text;
            pRow["HOUSETYPETRAIT"] = this.txtHOUSETYPETRAIT.Text;

             pRow["batch"]=this.cmbBatch.Text;

            try
            {
                pRow["housearearange"] = Double.Parse(this.txtHOUSEAREARANGE.Text);
            }
            catch (FormatException exp)
            {
                if (this.txtHOUSEAREARANGE.Text != "")
                {
                    MessageBox.Show("户型面积范围应为数值类型！", "楼盘信息编辑");
                    return;
                }
            }

            try
            {
                pRow["housearearange2"] = Double.Parse(this.txtHOUSEAREARANGE2.Text);
            }
            catch (FormatException exp)
            {
                if (this.txtHOUSEAREARANGE2.Text != "")
                {
                    MessageBox.Show("户型面积范围应为数值类型！", "楼盘信息编辑");
                    return;
                }
            }

            



            if (this.m_FormType == FormType.NEW)
            {
               // pDataSet.Tables["mainhousetype"].Columns.Remove("id");
                m_pMainHouseTypeTable.Rows.Add(pRow);

            }

           // pSqlDataAdp.Update(pDataSet, "mainhousetype");

           // pDataSet = new DataSet();
            //pSqlDataAdp.Fill(pDataSet, "mainhousetype");
       
           // m_pMainHouseTypeTable = pDataSet.Tables["mainhousetype"];

            this.Close();

        }
    }
}